/**********************************************************

@description:binary search tree
*File Name:bst.c
*Author: ydx
*mail: yudongxiang93@163.com
*Create Time: Sun 27 Apr 2025 12:43:43 AM CST
**********************************************************/
#include<stdio.h>
#include<stdlib.h>
typedef struct tnode{
	int data;
	struct tnode *lchild;
	struct tnode *rchild;
}tnode;
tnode* init_tnode(int data){
	tnode* n=(tnode*)malloc(sizeof(tnode));
	if(!n){
		printf("init_tnode malloc error!");
		return NULL;
	}
	n->data=data;
	n->lchild=NULL;
	n->lchild=NULL;
	return n;
}
tnode* bstSearch(tnode* t,int data){
	if(!t)
		return NULL;
	if(t->data==data){
		return t;
	}else if(data<t->data){
		bstSearch(t->lchild,data);
	}else{
		bstSearch(t->rchild,data);
	}
}
void bstInsert(tnode **t,int data){
	if(!(*t)){
		tnode* n=init_tnode(data);
		*t=n;
	}else if(data<(*t)->data){
		bstInsert(&(*t)->lchild,data);
	}else if(data>(*t)->data){
		bstInsert(&(*t)->rchild,data);
	}
}

typedef void (*visit)(tnode*);
void pn(tnode* n){
	if(n)
		printf("%d ",n->data);
}
void preorder(tnode* t,visit v){
	v(t);
	if(t){
		preorder(t->lchild,v);
		preorder(t->rchild,v);
	}
}
int main(int argc,char **argv){
	tnode *t=NULL;
//	int nums[6]={4,5,19,23,2,8};
	int nums[6]={8,6,10,9,11,23};
	for(int i=0;i<6;i++){
		bstInsert(&t,nums[i]);
	}
	preorder(t,pn);
	printf("\n");
}
